// 加载FORM表单扩展
layui.use(['form'], function(){
    var form = layui.form()
        ,layer = layui.layer;
	
	// 设置图片宽高
	function setImageSize(imageURL) {
		// 创建对象
		var image = new Image();
		 
		// 改变图片的src
		image.src = imageURL
		// 判断是否有缓存
		if(image.complete){
			$(".ad_width").val(image.width);
			$(".ad_height").val(image.height);
		}else{
			// 加载完成执行
			image.onload = function(){
				$(".ad_width").val(image.width);
				$(".ad_height").val(image.height);
			};
		}
	}

	// 是否开启水印
	var uploadBaseURL = '/upload/imageOne';

	// 初始化上传组件
	var uploader = new plupload.Uploader({
		browse_button : 'uploadImage',
		url : uploadBaseURL + "?isWatermark=1",
		flash_swf_url : 'js/Moxie.swf',
		silverlight_xap_url : 'js/Moxie.xap',
		multi_selection: false,
		filters: {
		  mime_types : [ //只允许上传图片文件和rar压缩文件
		    { title : "图片文件", extensions : "jpg,gif,png,bmp" }
		  ],
		  max_file_size : '500kb',
		  prevent_duplicates : true //不允许队列中存在重复文件
		}
	});
	
	uploader.init(); //初始化
  
	//绑定文件添加进队列事件
	uploader.bind('FilesAdded',function(uploader,files){	
		uploader.start();
	});

	//绑定文件上传进度事件
	uploader.bind('UploadProgress',function(uploader,file){
		$("#uploadImage").text('上传中 ' + file.percent + '%' );
	});
	
	uploader.bind('FileUploaded',function(uploader,file,responseObject){

		if( responseObject.status == 200 ) {
			var response = JSON.parse(responseObject.response);
			$(".preview-content").html("<img src='"+ response.url +"'>");
			$(".image_url").val(response.url);

			setImageSize( response.url );
		}
		
		$("#uploadImage").text('重新上传');
	});
	
  
    //自定义验证规则
    form.verify({
        ad_name: function(value){
            if(+value < 1) {
                return '物料名称不能为空';
            }

        }
        ,image_describe: function(value){
            if(value.length < 1) {
                return '图片描述不能为空';
            }

        }
        ,show_link: function(value){
        	var linkVal = $('input[name="is_game"]:checked ').val();
           if( +linkVal <= 0 && +value <= 0 ) {
				return '图片链接不能为空';
			}
        }
        ,game_id: function(value) {
			var gameVal = $('input[name="is_game"]:checked ').val();

			if( +gameVal > 0 && +value <= 0 ) {
				return '游戏id不能为空';
			}
		}
		,image_url: function(value){
			if(value.length < 1) {
                return '请上传图片文件';
            }
		}
        ,image_title: function(value){
            if(+value < 1) {
                return '图片标题不能为空';
            }
        }
		,ad_width: function(value){
			if(+value < 1) {
                return '请输入图片宽度';
            }
		}
		,ad_height: function(value){
			if(+value < 1) {
                return '请输入图片高度';
            }
		}
    });

    form.on('radio(is_game)', function(data){
		if(data.value==1){
			$(".game-false").hide();
			$(".game-box").show();
		}else{
			$(".game-false").show();
			$(".game-box").hide();
		}
	});

    //监听指定开关
    form.on('switch(watermark)', function(data){
    	var msg = "";
    	var uploadURL = "";
    	if( !!this.checked ) {
            uploadURL = uploadBaseURL + "?isWatermark=1";
            msg = "已开启自动添加水印";
		} else {
            uploadURL = uploadBaseURL + "?isWatermark=0";
            msg = "已关闭自动添加水印";
		}

        uploader.settings.url = uploadURL;
        layer.tips('温馨提示：' + msg , data.othis)
    });

    //监听提交
    form.on('submit(submit)', function(data){
    	var is_game =$("input[name=is_game]").val();
    	if(is_game==1){
            var game_id =$("#select2").val();
            if (typeof game_id !== 'undefined' && typeof game_id !== 'null') {
                data.field.game_id = game_id.join(',');
            }
		}
        $.ajax({
            url: "/Ad/create",
            type: "post",
            data: data.field ,
            dataType: "json",
            success: function (response) {
                if(response.status == 'success') {
                    PopModel.refreshParentFrame();
                } else {
                    layer.msg(response.msg, {icon: 5, shift: 6}, function(){});
                }
            }
        });
    });

	// 刷新表单
	form.render();
});
